package tx.bjdacp.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bjdacp.model.*;
import tx.bjdacp.dao.*;
import tx.bjdacp.service.*;
import tx.bjdacp.vo.query.*;


public class ResendLicLogAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/pages/ResendLicLog/query.jsp";
	protected static final String LIST_JSP= "/pages/ResendLicLog/list.jsp";
	protected static final String CREATE_JSP = "/pages/ResendLicLog/create.jsp";
	protected static final String EDIT_JSP = "/pages/ResendLicLog/edit.jsp";
	protected static final String SHOW_JSP = "/pages/ResendLicLog/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/ResendLicLog/list.do";

	private ResendLicLogManager resendLicLogManager;

	private ResendLicLog resendLicLog;
	java.lang.Integer id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			resendLicLog = new ResendLicLog();
		} else {
			resendLicLog = (ResendLicLog)resendLicLogManager.getById(id);
		}
	}

	private DrugSellUnitManager drugSellUnitManager;
	public void setDrugSellUnitManager(DrugSellUnitManager manager) {
		this.drugSellUnitManager = manager;
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setResendLicLogManager(ResendLicLogManager manager) {
		this.resendLicLogManager = manager;
	}

	public Object getModel() {
		return resendLicLog;
	}

	public void setBjdacpResendLicLogId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		ResendLicLogQuery query = newQuery(ResendLicLogQuery.class,DEFAULT_SORT_COLUMNS);

		Page page = resendLicLogManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
		DrugSellUnit drugSellUnit =drugSellUnitManager.getById(resendLicLog.getDrugSellUnitId());
		ResendLicLog resendLicLog_new=new ResendLicLog();
//		resendLicLog_new.setLicCode(resendLicLog.getLicCode());
//		resendLicLog_new.setLicGiveDate(resendLicLog.getLicGiveDate());
//		resendLicLog_new.setLicExpiryDate(resendLicLog.getLicExpiryDate());
		resendLicLog_new.setResendDate(resendLicLog.getResendDate());
		resendLicLog_new.setLicType(resendLicLog.getLicType());
		resendLicLog_new.setDrugSellUnitId(resendLicLog.getDrugSellUnitId());

		if(resendLicLog.getLicType().equals("许可证")){
			resendLicLog_new.setLicCode(drugSellUnit.getLicenceCode());
			resendLicLog_new.setLicGiveDate(drugSellUnit.getLicenceGiveDate());
			resendLicLog_new.setLicExpiryDate(drugSellUnit.getLicenceExpiryDate());

			drugSellUnit.setLicenceCode(resendLicLog.getLicCode());
			drugSellUnit.setLicenceGiveDate(resendLicLog.getLicGiveDate());
			drugSellUnit.setLicenceExpiryDate(resendLicLog.getLicExpiryDate());

		}else if(resendLicLog.getLicType().equals("GSP认证")){
			resendLicLog_new.setLicCode(drugSellUnit.getGspCode());
			resendLicLog_new.setLicGiveDate(drugSellUnit.getGspGiveDate());
			resendLicLog_new.setLicExpiryDate(drugSellUnit.getGspExpiryDate());

			drugSellUnit.setGspCode(resendLicLog.getLicCode());
			drugSellUnit.setGspGiveDate(resendLicLog.getLicGiveDate());
			drugSellUnit.setGspExpiryDate(resendLicLog.getLicExpiryDate());
		}

		resendLicLogManager.save(resendLicLog_new);
		drugSellUnitManager.saveOrUpdate(drugSellUnit);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		resendLicLogManager.update(this.resendLicLog);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Integer id = new java.lang.Integer((String)params.get("bjdacpResendLicLogId"));
			resendLicLogManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

}
